package com.canbot.u05.activity.customvoice.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.customvoice.bean.CustomDialogData;

import java.util.List;

/**
 * Created by liu.heng on 2018/5/25.
 */

public class CustomDialogAdapter extends RecyclerView.Adapter<CustomDialogAdapter.ViewHolder> {

        private Context context;

        private List<CustomDialogData> mList;

        private LayoutInflater mLayoutInflater;

        private int checked = -1;

        private OnItemClickListener mOnItemClickListener;

        public interface OnItemClickListener {

                void onItemClick(View view);
        }

        public CustomDialogAdapter(Context context, List<CustomDialogData> list, OnItemClickListener onItemClickListener) {
                this.context = context;
                this.mList = list;
                mLayoutInflater = LayoutInflater.from(context);
                this.mOnItemClickListener = onItemClickListener;
        }

        public void setChecked(int checked) {//设定一个选中的标志位，在activity中传入值。
                this.checked = checked;
        }

        public int getChecked() {
                return checked;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View inflate = mLayoutInflater.inflate(R.layout.custom_dialog_tiem, parent, false);
                return new ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                if (checked == position) {
                        holder.check_Image.setVisibility(View.VISIBLE);
                }
                else {
                        holder.check_Image.setVisibility(View.GONE);
                }
                holder.action_Image.setBackgroundResource(R.drawable.dialog_pic);
                holder.action_text.setText(mList.get(position).getName());
                holder.mRelativeLayout.setTag(position);
                holder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                mOnItemClickListener.onItemClick(view);
                        }
                });
        }

        @Override
        public int getItemCount() {
                return mList == null ? 0 : mList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {

                private RelativeLayout mRelativeLayout;

                private ImageView action_Image;

                private ImageView check_Image;

                private TextView action_text;

                public ViewHolder(View view) {
                        super(view);
                        action_Image = (ImageView) view.findViewById(R.id.custem_image);
                        check_Image = (ImageView) view.findViewById(R.id.check_image);
                        action_text = (TextView) view.findViewById(R.id.dialog_text);
                        mRelativeLayout = (RelativeLayout) view.findViewById(R.id.my_relativeLayout);
                }
        }
}
